package com.fishery.processing.mapper;

import com.fishery.processing.entity.Input;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fishery.processing.entity.vo.InputVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author ${author}
 * @since 2021-01-14
 */
public interface InputMapper extends BaseMapper<Input> {

    @Select("SELECT pi.* FROM t_processing_input pi INNER JOIN t_processing_craft_input pci WHERE pci.craft_id = #{craftId} AND pi.id = pci.input_id LIMIT #{startNo},#{size}")
    public List<Input> findInputsByCraft(@Param("craftId") String craftId,@Param("startNo") Integer startNo
                                            ,@Param("size") Integer size);

    @Select("SELECT pi.* FROM t_processing_input pi INNER JOIN t_processing_craft_input pci WHERE pci.craft_id = #{craftId} AND pi.id = pci.input_id;")
    public List<Input> findAllInputsByCraft(@Param("craftId") String craftId);

    @Insert("INSERT INTO t_processing_craft_input VALUES(#{craftId},#{inputId});")
    public int addInputOfCraft(@Param("craftId") String craftId,@Param("inputId") String inputId);

    @Delete("DELETE FROM t_processing_craft_input WHERE craft_id = #{craftId} AND input_id = #{inputId};")
    public int removeInputOfCraft(@Param("craftId") String craftId,@Param("inputId") String inputId);

    //删除工艺与所有投入品的连接表信息
    @Delete("DELETE FROM t_processing_craft_input WHERE craft_id = #{craftId}")
    public int removeAllInputOfCraft(@Param("craftId") String craftId);

    @Select("SELECT pi.* FROM t_processing_input pi INNER JOIN t_processing_craft_input pci WHERE pci.craft_id = #{craftId} AND pci.input_id = #{inputId} AND pi.id = pci.input_id")
    public Input findInputByCraftAndInput(@Param("craftId") String craftId,@Param("inputId") String inputId);

    @Select("SELECT COUNT(*) FROM t_processing_craft_input WHERE craft_id = #{craftId}")
    public long findInputCountOfCraft(@Param("craftId") String craftId);

    //删除加工厂中所有投入品的连接表信息
    @Delete("DELETE FROM t_processing_craft_input WHERE input_id IN (SELECT id FROM t_processing_input WHERE processing_factory_id = #{factoryId});")
    public int removeInputOfFactory(@Param("factoryId") String factoryId);

    @Select("SELECT id, input_name, input_produce_date, input_expire_date, inspector, supplier_name, supplier_addr" +
            ", supplier_phone, supplier_license, input_picture, specification,  processing_factory_id FROM t_processing_input tpi " +
            "INNER JOIN t_processing_craft_input tpci ON tpi.id = tpci.input_id " +
            "AND tpci.craft_id = #{craftId}")
    public List<InputVo> getInputsOfCraft(String craftId);
}
